package com.gsta.cdn.cdr.controller;

import com.gsta.cdn.cdr.entity.ResultEntity;
import com.gsta.cdn.cdr.mapper.CIDRetainMapper;
import com.gsta.cdn.cdr.mapper.FileSizeMapper;
import com.gsta.cdn.cdr.service.CIDRetainService;
import com.gsta.cdn.cdr.service.FileSizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/CIDRetain")
public class CIDRetainController {
    @Autowired
    private CIDRetainMapper cidRetainMapper;

    @Resource
    private CIDRetainService cidRetainService;

    //全局分热度存留统计
    @RequestMapping(path = "/CIDRetainAll", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity CIDRetainAll(@RequestParam(required = true) String date,
                                    @RequestParam(required = true) String percent,
                                    @RequestParam(required = false,defaultValue = "1")int page,
                                    @RequestParam(required = false,defaultValue = "200")int pageSize,
                                    HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidRetainService.CIDRetainAll(date,percent,page,pageSize);
        int total = cidRetainMapper.CIDRetainAllCount(date,percent);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //热度存储地区列表
    @RequestMapping(path = "/CIDRetainArea", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ResolutionTypeArea(@RequestParam(required = true) String date,
                                           @RequestParam(required = true) String percent,
                                           HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidRetainService.CIDRetainArea(date,percent);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //地区热度存留统计
    @RequestMapping(path = "/CIDRetainByArea", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ResolutionTypeByArea(@RequestParam(required = true) String date,
                                             @RequestParam(required = true) String percent,
                                             @RequestParam(required = true) String area,
                                             @RequestParam(required = false,defaultValue = "1")int page,
                                             @RequestParam(required = false,defaultValue = "200")int pageSize,
                                             HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidRetainService.CIDRetainByArea(date,percent,area,page,pageSize);
        int total = cidRetainMapper.CIDRetainByAreaCount(date,percent,area);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //PopName热度存留统计
    @RequestMapping(path = "/CIDRetainByPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity CIDRetainByPopName(@RequestParam(required = true) String date,
                                          @RequestParam(required = true) String percent,
                                          @RequestParam(required = true) String popName,
                                          @RequestParam(required = false,defaultValue = "1")int page,
                                          @RequestParam(required = false,defaultValue = "200")int pageSize,
                                          HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidRetainService.CIDRetainByPopName(date,percent,popName,page,pageSize);
        int total = cidRetainMapper.CIDRetainByPopNameCount(date,percent,popName);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //地区的PopName列表
    @RequestMapping(path = "/CIDRetainAreaPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity CIDRetainAreaPopName(@RequestParam(required = true) String date,
                                            @RequestParam(required = true) String percent,
                                            @RequestParam(required = true) String area,
                                            HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidRetainService.CIDRetainAreaPopName(date,percent,area);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //地区按PopName存储统计
    @RequestMapping(path = "/CIDRetainByAreaPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity CIDRetainByAreaPopName(@RequestParam(required = true) String date,
                                              @RequestParam(required = true) String percent,
                                              @RequestParam(required = true) String area,
                                              @RequestParam(required = true) String popName,
                                              @RequestParam(required = false,defaultValue = "1")int page,
                                              @RequestParam(required = false,defaultValue = "200")int pageSize,
                                              HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidRetainService.CIDRetainByAreaPopName(date,percent,area,popName,page,pageSize);
        int total = cidRetainMapper.CIDRetainByAreaPopNameCount(date,percent,area,popName);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }
}
